var DingDanTable;
var dingDanStates={'0':'下单','1':'完成','2':'指派','3':'作废','4':'<font color=#f00>超期</font>'};//,'5':'核实'
/****列表信息*/
function showDingDans() {
	showLoading("查询中...");
	$.ajax( {
		url : 'dingDan/initQuery.do',
		type : 'get',
		dataType : 'html',
		async:true,
		success : function(html) {
			$('#mainForm').html(html);
			 queryDingDanDatas();
			 hideLoading();
			//绑定查询按钮 
		$('#searchDingDanBtn').click(function() {
			queryDingDanList();
		});
		$(".comboBox").kendoComboBox({filter:"contains", suggest:true });
	}
	});
}
function queryDingDanDatas(){
		DingDanTable = $('#entTable').table( {
				width : '100%',
				form : {
					url : 'dingDan/queryList.do',
					id : '#entSearchForm',
					type : 'post',
					pageNumberText : '#pageNum'
				},
				pageable : true,
				selectable : true,
				colums : [ {
					columnName : '订单编号',
					name : 'dingDanUid',
					handle : "toSearchDingDan"
				},{
					columnName : '门店',
					name : 'mendianName',sortable:false
				},
				{
					columnName : '电池品牌',
					name : 'pinPai',sortable:false
				}, {
					columnName : '电池型号',
					name : 'xingHao',sortable:false
				}
//				, {
//					columnName : '价格',
//					name : 'price'
//				}
				,  {
					columnName : '更换方式',
					name : 'changeType',sortable:false
				}
//				, {
//					columnName : '客户地址',
//					name : 'address'
//				}, {
//					columnName : '预约时间',
//					name : 'time'
//				}
				,{
					columnName : '服务站',
					name : 'fuwuzhanName',sortable:false
				}
				, {
					columnName : '订单状态',
					name : 'state',
					formatter:function(obj){ return dingDanStates[obj.state];}
				}, {
					columnName : '操作',width:"60px",
					name : '',
					sortable:false,
					formatter:function(obj){return '<input type="button" value="查看"   style="cursor: pointer" onclick="toAddDingDan('+obj.id+',\''+obj.changeType+'\','+obj.state+')">'}
				}
				]
			});
}
function queryDingDanList(){
	showLoading("查询中...");
	queryDingDanDatas();
	hideLoading();
}

function openDialog(st){
	$("#hiddenState").val(st);
	$.get('dingDan/zhipai.do', function(html) {
		$('#kuDialogDiv').dialog( {
						modal : true,
						title : '请选择门店',
						width : 755,
						height : 460,
						open:function(event, ui){
			               $('#kuDialogDiv').html(html);
			               queryZhipaiMendian();
						},
						close:function(event, ui){
			               //
							$('#kuDialogDiv').dialog("destroy");
							$('#kuDialogDiv').remove();
							$("#parentDiv").html("<div id='kuDialogDiv' style='display: none'></div>");
						
						}
		});
});
}
function queryZhipaiMendian(){
   $('#entTable2').table({
		width:'100%',
		form:{
				url:'menDian/queryMendianPageList.do',				
				id:'#entSearchForm1',					
				type:'post',
				pageNumberText:"#pageNum"
		},
		pageable:true,								
		selectable:false,							
		colums:[
				
				{columnName:'编号',width:'25px',name:'id',sortable:false},
				{columnName:'门店名称',width:'130px',name:'name',sortable:false},
				{columnName:'地址',width:'160px',name:'address',sortable:false},
				
				{columnName:'联系人',width:'40px',name:'lxr',sortable:false},
				{columnName:'联系电话',width:'100px',name:'lxrTel',sortable:false},
				{columnName:'操作',name:'', width:'40px',sortable:false,formatter:function(obj){
		  			return "<input type='button' style='cursor: pointer' value='选择' onclick='zhipaiMengDian("+obj.id+",\""+obj.address+"\",\""+obj.name+"\",\""+obj.lxr+"\")'>";
		  		},
		  		sortable:false}
			   ]
		});

}
function chaxun(){
	queryZhipaiMendian();
}


//动态加载门店数据
function zhipaiMengDian(id,dizhi,name,lxr){
	$('#model_address').val(dizhi);
	$('#addressId').html(dizhi);
	$('#mendianIds').val(id);
	$('#state1').html("已指派");
	$('#kuDialogDiv').dialog('close');
	$('#model_mendianLxr').val(lxr);
	$('#mdlxrId').html(lxr);
	$('#model_mendianName').val(name);
	$('#mdmcId').html(name);
	$('#kuDialogDiv').remove();
	$('#DingDanForm').ajaxSubmit( {
			url : 'dingDan/saveDingDan.do',
			type : 'post',
			dataType : 'json',
			success : function(data) {
				if (data.success) {
					showDialog('操作成功');
					$('#commonDialogDiv').dialog('close');
					DingDanTable.refresh();
				} else {
					showDialog('保存失败' + data.message);
				}
			}
		});
}

//去查看
function toSearchDingDan(id) {
	var param = "";
	if (id) {
		param='?id=' + id;
	}
	$.get('dingDan/toAddDingDan.do'+param, function(html) {
		$('#commonDialogDiv').html(html);
		$('#commonDialogDiv').dialog( {
			modal : true,
			title : '订单信息表',
			width : 900,
			height : 600,
			buttons : {
				'取消' : function() {
					$(this).dialog('close');
				}
			},close:function(){$(this).dialog('destroy');}
		});
	});
}


//去增加
function toAddDingDan(id,genghuan,state) {
	var param = '?s=' + ts();
	if (id) {
		param += '&id=' + id;
	}
	$.get('dingDan/toAddDingDan.do' + param, function(html) {
		$('#commonDialogDiv').html(html);
		if('1'==state||'3'==state){//已经完成的只能查看
		btns={'取消' : function() {$(this).dialog('close');}};
	}else{
		if('附近门店更换'==(genghuan.trim())){
		btns={'作废' : function() {$("#hiddenState").val('3');saveAddDingDan();},'取消' : function() {$(this).dialog('close');}};
	   }else{
		   btns={'指派' : function() {openDialog('2');},
				'作废' : function() {$("#hiddenState").val('3');saveAddDingDan();},
				'取消' : function() {$(this).dialog('close');}};
		   
//		 if('0'==$("#hiddenUserType").val()){
//			 btns={'核实' : function() {openDialog('5');},
//				'作废' : function() {$("#hiddenState").val('3');saveAddDingDan();},
//				'取消' : function() {$(this).dialog('close');}};
//		 }else{
//			 btns={'指派' : function() {openDialog('2');},
//				'作废' : function() {$("#hiddenState").val('3');saveAddDingDan();},
//				'取消' : function() {$(this).dialog('close');}};
//		 }
	   }
	}
		$('#commonDialogDiv').dialog( {
			modal : true,
			title : '订单信息表',
			width : 900,
			height : 600,
			buttons : btns,close:function(){$(this).dialog('destroy');}
		});
	});
}
//去保存（改状态）
function saveAddDingDan() {
		$('#DingDanForm').ajaxSubmit( {
			url : 'dingDan/saveDingDan.do',
			type : 'post',
			dataType : 'json',
			success : function(data) {
				if (data.success) {
					showDialog('处理成功');
					$('#commonDialogDiv').dialog('close');
					DingDanTable.refresh();
				} else {
					showDialog('保存失败' + data.message);
				}
			}
		});
}

//批量删除 
function toDeleteDingDan() {
	var ids = '';
	$('.ids').each(function() {
		if (this.checked == true) {
			ids += 'ids=' + this.value + '&';
		}
	});
	if (ids.length == 0) {
		showDialog('请至少选择一条记录！');
		return;
	}
	$('#commonDialogDiv').html('<p>确定要删除记录信息？</p>');
	$('#commonDialogDiv').dialog( {
		modal : true,
		width : 250,
		height : 160,
		buttons : {
			'确定' : function() {
				$(this).dialog('close');
				$.ajax({
					url : 'dingDan/delete.do?' + ids,
					type : 'get',
					dataType : 'json',
					success : function(data) {
						if (data.success) {
							DingDanTable.refresh();
						} else {
							showDialog('删除失败，原因：' + data.message);
						}
					}
				});
			},
			'取消' : function() {
				$(this).dialog('close');
			}
		},close:function(){$(this).dialog('destroy');}
	});
}
function exportDingdanExcel(){
	//showLoading('正在导出，请稍后...');
	$("#hDingDanUid").val($("#disDingDanUid").val());
	$("#hState").val($("#disState").val());
	$("#hChangeType").val($("#disChangeType").val());
	$("#dingDanExcelForm").attr("action","dingDan/exportExcel.do").submit();
	//hideLoading();
}

//去查看
function toSearchDingDan(id) {
	var param = "";
	if (id) {
		param='?id=' + id;
	}
	$.get('dingDan/toAddDingDan.do'+param, function(html) {
		$('#commonDialogDiv').html(html);
		$('#commonDialogDiv').dialog( {
			modal : true,
			title : '订单信息表',
			width : 900,
			height : 600,
			buttons : {
				'取消' : function() {
					$(this).dialog('close');
				}
			},close:function(){$(this).dialog('destroy');}
		});
	});
}
